﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using MODEL;
using System.Data;
namespace DAL
{
    public class DalHistoryStock
    {
        public int IsExistByDate(DateTime date)
        {
            string sql = "select id from historystock where createyear=@year and createmonth=@month";
            return Convert.ToInt32(AccessHelp.ExecuteScalar(sql,new OleDbParameter("@year",date.Year.ToString()),new OleDbParameter("@month",date.Month.ToString())));
        }

        public int Insert(DateTime dt)
        {
            string hbsql = "insert into historystock(createyear,createmonth) values(@year,@month)";
            OleDbParameter[] hbpms = new OleDbParameter[]
                    {
                        new OleDbParameter("@year",dt.Date.Year.ToString()),
                        new OleDbParameter("@month",dt.Date.Month.ToString()) 
                    };
            return AccessHelp.ExecteNonQuery(hbsql, hbpms);

        }
        public List<historystock> GetAllList()
        {
            string sql = "select id,createyear,createmonth from historystock order by createyear,createmonth";
            DataTable dt = AccessHelp.ExecuteReader(sql,null);
            return TableToList(dt);
        }
        List<historystock> TableToList(DataTable dt)
        {
            List<historystock> hstocks = new List<historystock>();
            foreach (DataRow row in dt.Rows)
            {
                historystock hstock = new historystock();
                hstock.Id = Convert.ToInt32(row[0]);
                hstock.Date = new DateTime(Convert.ToInt32(row[1]),Convert.ToInt32(row[2]),1);
                hstocks.Add(hstock);
            }
            return hstocks;
        }
    }
}
